<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	public function manageUser()
	{
	
		// test
		$data["positionAll"] = $this->user_model->getAllPosition();
		$data["departmentAll"] = $this->user_model->getAllDepartment();
		$data["factoryAll"] = $this->user_model->getAllFactory();
		
		$data['rs']=$this->user_model->allUser();

		//$this->db->select('*');
		//$this->db->from('user');
		//$this->db->join('position','position.position_id = user.position_id');
		//$this->db->join('department','department.department_id = user.department_id');
		//$this->db->join('factory','factory.factory_id = user.factory_id');
		//$rs= $query = $this->db->get();
		//$this->db->order_by("user_id","asc"); 
		//$rs = $this->db->get("user");
		//$data['rs'] = $rs->result_array();

		
		$this->load->view('template/template');
		$this->load->view('user/manageUser',$data);
		$this->load->view('template/footer');
		
	 
	}
	public function position()
	{
		$this->load->view('template/template');
		$this->load->view('user/position');
		$this->load->view('template/footer');
	}
	public function department()
	{
		$this->load->view('template/template');
		$this->load->view('user/department');
		$this->load->view('template/footer');
	}
	public function permission()
	{
		$this->load->view('template/template');
		$this->load->view('user/permission');
		$this->load->view('template/footer');
	}		
	
	///////////////////////////////////////////////
	
	public function getAllUser(){
		echo $this->user_model->getUser();
	}
		
	public function deleteUser($user_id)
	{
			$user_id = $this->uri->segment(3);
			$this->user_model->delUserDB($user_id);
	}
	
	
	public function addUser(){
		$data = array(
				"user_username" => $this->input->post("username"),
				"user_password" => md5($this->input->post("password")),
				"user_fname" => $this->input->post("fname"),
				"user_lname" => $this->input->post("lname"),
				"position_id" => $this->input->post("position"),
				"department_id" => $this->input->post("department"),
				"factory_id" => $this->input->post("factory"),
				"user_email" => $this->input->post("email"),
				"user_mobile" => $this->input->post("mobile"),
				"user_status" => 1,
				);
		echo $this->user_model->addUserDB($data);
		//jprint_r($data);
		//$data = $this->input->post("models");
		//print_r($data);
		//$id = size_of($data);
		/*$id=0;
		echo "xxxx".$id;
		echo $data[$id]["user_fname"];
		echo $data[$id]["user_lname"];
		echo $data[$id]["user_username"];
		echo $data[$id]["user_email"];
		echo $data[$id]["user_mobile"];
		*/
	//	print_r($_POST["user_email"]);
		/*
	    echo $this->input->post("user_username");
	 	echo $this->input->post("user_fname");
	 	print_r($_POST["models"]);
	 	
		//print_r(json_decode($_GET));
		print_r(json_decode($_POST["models"]));
		$data = json_decode($_POST["models"]);
		
	echo $data["user_name"];
	*/
		//print_r($data);
	}
	public function allUser()
	{
			
			$user_id = $this->uri->segment(3);
			$data['rs']=$this->user_model->allUserDB($user_id);
			$this->load->view('template/template');
			$this->load->view('user/detailUser',$data);
			$this->load->view('template/footer');
	}
	public function updateUser()
	{
			$input=array(
					"user_id"=>$this->input->post("user_id"),
					"user_username"=>$this->input->post("user_username"),
					"user_fname"=>$this->input->post("user_fname"),
					"user_lname"=>$this->input->post("user_lname"),
					"user_email"=>$this->input->post("user_email"),
					"user_mobile"=>$this->input->post("user_mobile"),
					"position_id"=>$this->input->post("position_id"),
					"department_id"=>$this->input->post("department_id"),
					"factory_id"=>$this->input->post("factory_id")
			);
			//echo "<pre>";
			//print_r($input);
			$this->user_model->updateUserDB($input);
	}
	//////////////////////////////////////////////////
}
 